<?php
//开启Session
session_start();
//设置时区
date_default_timezone_set("Asia/Shanghai");
function Random_String($length)
{
    $str = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
    shuffle($str);
    $str = implode('', array_slice($str, 0, $length));
    return $str;
}

//Redis
function Redis_Link()
{
    global $redis_address;
    global $redis_port;
    global $redis_auth;
    $redis = new Redis();
    $redis->pconnect($redis_address, $redis_port);
    $redis->auth($redis_auth);
    $redis->select(1);
    return $redis;
}

//Mysql
function DB_Link()
{
    global $mysql_address;
    global $mysql_port;
    global $mysql_username;
    global $mysql_password;
    global $mysql_db_name;
    $db_link = mysqli_connect($mysql_address, $mysql_username, $mysql_password, $mysql_db_name, $mysql_port);
    if (!$db_link) {
        echo "Mysql Error";
        exit;
    } else {
        mysqli_query($db_link, "SET NAMES utf8");
        return $db_link;
    }
}

//Get Config
function Get_Config($name)
{
    $redis = Redis_Link();
    $result = $redis->get('Config_' . $name);
    if (empty($result)) {
        $db_link = DB_Link();
        $row_config = mysqli_fetch_array(mysqli_query($db_link, "SELECT * FROM setting WHERE name = '" . $name . "'"));
        if (empty($row_config)) {
            return "0";
        } else {
            $redis->set('Config_' . $name, $row_config['data']);
            return $row_config['data'] == "" ? 0 : $row_config['data'];
        }
    } else {
        return $result;
    }
}
//登录状态检查
function Login_Status(){
    if ($_SESSION['login_status']==1){
        return true;
    }else{
        return false;
    }
}
//获取系统负载
class SystemInfoWindows
{
    /**
     * 判断指定路径下指定文件是否存在，如不存在则创建
     * @param string $fileName 文件名
     * @param string $content 文件内容
     * @return string 返回文件路径
     */
    private function getFilePath($fileName, $content)
    {
        $path = dirname(__FILE__) . "\\$fileName";
        if (!file_exists($path)) {
            file_put_contents($path, $content);
        }
        return $path;
    }
    /**
     * 获得cpu使用率vbs文件生成函数
     * @return string 返回vbs文件路径
     */
    private function getCupUsageVbsPath()
    {
        return $this->getFilePath(
            'cpu_usage.vbs',
            "On Error Resume Next
    Set objProc = GetObject(\"winmgmts:\\\\.\\root\cimv2:win32_processor='cpu0'\")
    WScript.Echo(objProc.LoadPercentage)"
        );
    }
    /**
     * 获得总内存及可用物理内存JSON vbs文件生成函数
     * @return string 返回vbs文件路径
     */
    private function getMemoryUsageVbsPath()
    {
        return $this->getFilePath(
            'memory_usage.vbs',
            "On Error Resume Next
    Set objWMI = GetObject(\"winmgmts:\\\\.\\root\cimv2\")
    Set colOS = objWMI.InstancesOf(\"Win32_OperatingSystem\")
    For Each objOS in colOS
     Wscript.Echo(\"{\"\"TotalVisibleMemorySize\"\":\" & objOS.TotalVisibleMemorySize & \",\"\"FreePhysicalMemory\"\":\" & objOS.FreePhysicalMemory & \"}\")
    Next"
        );
    }
    /**
     * 获得CPU使用率
     * @return Number
     */
    public function getCpuUsage()
    {
        $path = $this->getCupUsageVbsPath();
        exec("cscript -nologo $path", $usage);
        return $usage[0];
    }
    /**
     * 获得内存使用率数组
     * @return array
     */
    public function getMemoryUsage()
    {
        $path = $this->getMemoryUsageVbsPath();
        exec("cscript -nologo $path", $usage);
        $memory = json_decode($usage[0], true);
        $memory['usage'] = Round((($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / $memory['TotalVisibleMemorySize']) * 100);
        return $memory;
    }
}
//API鉴权
function API_Auth($get,$post){
    if (empty($get)&&empty($post)){
        return false;
    }elseif(empty($get)){
        $api_key=$post;
    }else{
        $api_key=$get;
    }
    $real_api_key=Get_Config('api_key');
    if ($api_key==$real_api_key){
        return true;
    }else{
        return false;
    }
}
//删除目录及文件
function Del_Dir( $dirName )
{
    if ( $handle = opendir( "$dirName" ) ) {
        while ( false !== ( $item = readdir( $handle ) ) ) {
            if ( $item != "." && $item != ".." ) {
                if ( is_dir( "$dirName/$item" ) ) {
                    Del_Dir( "$dirName/$item" );
                }
            }
        }
        closedir( $handle );
    }
}
//更改系统设置
function Change_Config($name,$value){
    $db_link=DB_Link();
    $redis=Redis_Link();
    $row_config=mysqli_fetch_array(mysqli_query($db_link,"SELECT * FROM `setting` WHERE `name` = '".$name."'"));
    if (empty($row_config['ID'])){
        mysqli_query($db_link,"INSERT INTO  `setting` (`ID`,`name`,`data`) values (NULL,'".$name."','".$value."')");
        return true;
    }else{
        mysqli_query($db_link,"UPDATE `setting` SET `data` = '".$value."' WHERE `name` = '".$name."'");
        $redis->del('Config_'.$name);
        return true;
    }
}

/**
 * 分页类
 * 使用方式:
 * $page = new Page();
 * $page->init(1000, 20);
 * $page->setNotActiveTemplate('<span> {a} </span>');
 * $page->setActiveTemplate('{a}');
 * echo $page->show();
 *
 *
 * @author 风居住的地方
 */
class Page {
    /**
     * 总条数
     */
    private $total;
    /**
     * 每页大小
     */
    private $pageSize;
    /**
     * 总页数
     */
    private $pageNum;
    /**
     * 当前页
     */
    private $page;
    /**
     * 地址
     */
    private $uri;
    /**
     * 分页变量
     */
    private $pageParam;
    /**
     * LIMIT XX,XX
     */
    private $limit;
    /**
     * 数字分页显示
     */
    private $listnum = 8;
    /**
     * 分页显示模板
     * 可用变量参数
     * {total}   总数据条数
     * {pagesize}  每页显示条数
     * {start}   本页开始条数
     * {end}    本页结束条数
     * {pagenum}  共有多少页
     * {frist}   首页
     * {pre}    上一页
     * {next}    下一页
     * {last}    尾页
     * {list}    数字分页
     * {goto}    跳转按钮
     */
    private $template = '<div><span>共有{total}条数据</span><span>每页显示{pagesize}条数据</span>,<span>本页{start}-{end}条数据</span><span>共有{pagenum}页</span><ul>{frist}{pre}{list}{next}{last}{goto}</ul></div>';
    /**
     * 当前选中的分页链接模板
     */
    private $activeTemplate = '<li class="active"><a href="javascript:;">{text}</a></li>';
    /**
     * 未选中的分页链接模板
     */
    private $notActiveTemplate = '<li><a href="{url}">{text}</a></li>';
    /**
     * 显示文本设置
     */
    private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页');
    /**
     * 初始化
     * @param type $total    总条数
     * @param type $pageSize  每页大小
     * @param type $param    url附加参数
     * @param type $pageParam  分页变量
     */
    public function init($total, $pageSize, $param = '', $pageParam = 'page') {
        $this->total = intval($total);
        $this->pageSize = intval($pageSize);
        $this->pageParam = $pageParam;
        $this->uri = $this->geturi($param);
        $this->pageNum = ceil($this->total / $this->pageSize);
        $this->page = $this->setPage();
        $this->limit = $this->setlimit();
    }

    /**
     * 设置分页模板
     * @param type $template  模板配置
     */
    public function setTemplate($template) {
        $this->template = $template;
    }

    /**
     * 设置选中分页模板
     * @param type $activeTemplate   模板配置
     */
    public function setActiveTemplate($activeTemplate) {
        $this->activeTemplate = $activeTemplate;
    }

    /**
     * 设置未选中分页模板
     * @param type $notActiveTemplate  模板配置
     */
    public function setNotActiveTemplate($notActiveTemplate) {
        $this->notActiveTemplate = $notActiveTemplate;
    }

    /**
     * 返回分页
     * @return type
     */
    public function show() {
        return str_ireplace(array(
            '{total}',
            '{pagesize}',
            '{start}',
            '{end}',
            '{pagenum}',
            '{frist}',
            '{pre}',
            '{next}',
            '{last}',
            '{list}',
            '{goto}',
        ), array(
            $this->total,
            $this->setPageSize(),
            $this->star(),
            $this->end(),
            $this->pageNum,
            $this->frist(),
            $this->prev(),
            $this->next(),
            $this->last(),
            $this->pagelist(),
            $this->gopage(),
        ), $this->template);
    }

    /**
     * 获取limit起始数
     * @return type
     */
    public function getOffset() {
        return ($this->page - 1) * $this->pageSize;
    }

    /**
     * 设置LIMIT
     * @return type
     */
    private function setlimit() {
        return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}";
    }

    /**
     * 获取limit
     * @param type $args
     * @return type
     */
    public function __get($args) {
        if ($args == "limit") {
            return $this->limit;
        } else {
            return null;
        }
    }

    /**
     * 初始化当前页
     * @return int
     */
    private function setPage() {
        if (!empty($_GET[$this->pageParam])) {
            if ($_GET[$this->pageParam] > 0) {
                if ($_GET[$this->pageParam] > $this->pageNum)
                    return $this->pageNum;
                else
                    return $_GET[$this->pageParam];
            }
        }
        return 1;
    }

    /**
     * 初始化url
     * @param type $param
     * @return string
     */
    private function geturi($param) {
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param;
        $parse = parse_url($url);
        if (isset($parse["query"])) {
            parse_str($parse["query"], $params);
            unset($params["page"]);
            $url = $parse["path"] . "?" . http_build_query($params);
            return $url;
        } else {
            return $url;
        }
    }

    /**
     * 本页开始条数
     * @return int
     */
    private function star() {
        if ($this->total == 0) {
            return 0;
        } else {
            return ($this->page - 1) * $this->pageSize + 1;
        }
    }

    /**
     * 本页结束条数
     * @return type
     */
    private function end() {
        return min($this->page * $this->pageSize, $this->total);
    }

    /**
     * 设置当前页大小
     * @return type
     */
    private function setPageSize() {
        return $this->end() - $this->star() + 1;
    }

    /**
     * 首页
     * @return type
     */
    private function frist() {
        $html = '';
        if ($this->page == 1) {
            $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true);
        } else {
            $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false);
        }
        return $html;
    }

    /**
     * 上一页
     * @return type
     */
    private function prev() {
        $html = '';
        if ($this->page > 1) {
            $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true);
        }
        return $html;
    }

    /**
     * 分页数字列表
     * @return type
     */
    private function pagelist() {
        $linkpage = "";
        $lastlist = floor($this->listnum / 2);
        for ($i = $lastlist; $i >= 1; $i--) {
            $page = $this->page - $i;
            if ($page >= 1) {
                $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
            } else {
                continue;
            }
        }
        $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true);
        for ($i = 1; $i <= $lastlist; $i++) {
            $page = $this->page + $i;
            if ($page <= $this->pageNum) {
                $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
            } else {
                break;
            }
        }
        return $linkpage;
    }

    /**
     * 下一页
     * @return type
     */
    private function next() {
        $html = '';
        if ($this->page < $this->pageNum) {
            $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true);
        }
        return $html;
    }

    /**
     * 最后一页
     * @return type
     */
    private function last() {
        $html = '';
        if ($this->page == $this->pageNum) {
            $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false);
        }
        return $html;
    }

    /**
     * 跳转按钮
     * @return string
     */
    private function gopage() {
        $html = '';
        $html.=' <input type="text" value="' . $this->page . '" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" style="width:25px;"/><input type="button" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'" value="GO"/>';
        return $html;
    }

    /**
     * 模板替换
     * @param type $replace   替换内容
     * @param type $result   条件
     * @return type
     */
    private function replace($url, $text, $result = true) {
        $template = ($result ? $this->activeTemplate : $this->notActiveTemplate);

        $html = str_replace('{url}', $url, $template);
        $html = str_replace('{text}', $text, $html);
        return $html;
    }
}